package com.share.device.controller;

import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.common.log.annotation.Log;
import com.share.common.log.enums.BusinessType;
import com.share.common.security.annotation.RequiresPermissions;
import com.share.device.domain.CabinetType;
import com.share.device.service.ICabinetTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 柜机类型controller
 * 继承自定义 通用Controller BaseController
 */
@Tag(name = "柜机类型接口管理")
@RestController
@RequestMapping("/cabinetType")
public class CabinetTypeController extends BaseController {

    @Autowired
    private ICabinetTypeService cabinetTypeService;

    //分页查询柜机类型 分页查询返回TableDataInfo数据
    @Operation(summary = "查询柜机类型列表")
    @RequiresPermissions("device:cabinetType:list")
    @GetMapping("/list")
    public TableDataInfo list(CabinetType cabinetType){
        log.info("分页查询柜机类型");
        //底层使用PageHelper，将PageHelper的方法进行了二次封装 startPage方法 并且封装了 getDataTable 直接返回分页好的数据
        startPage();
        List<CabinetType> cabinetTypeList = cabinetTypeService.selectCabinetTypeList(cabinetType);
        return getDataTable(cabinetTypeList);
    }

    //根据id查询柜机详情 普通查询使用AjaxResult类返回数据
    @Operation(summary = "获取柜机类型详细信息")
    @RequiresPermissions("device:cabinetType:query")
    @GetMapping("/{id}")
    public AjaxResult getCabinetTypeById(@PathVariable Long id){
        log.info("获取柜机类型详细信息:{}",id);

        //使用mybatis-plus封装的getById方法快速查询
        CabinetType cabinetType = cabinetTypeService.getById(id);

        //success方法 底层 是返回的公共的json格式数据，包括状态码，信息，数据
        return AjaxResult.success(cabinetType);
    }

    //新增柜机类型
    @Operation(summary = "新增柜机类型")
    @RequiresPermissions("device:cabinetType:add") //交给spring security权限控制 有该权限的role才能使用
    @Log(title = "柜机类型" ,businessType = BusinessType.INSERT) //使用aop+自定义注解完成对方法的日志记录
    @PostMapping
    public AjaxResult save(@RequestBody CabinetType cabinetType){
        log.info("新增柜机类型:{}",cabinetType);

        //使用mp 快速新增
        boolean isSuccess = cabinetTypeService.save(cabinetType);

        //方法用于更新 删除等无返回数据的操作，判断是否成功 返回的仍然是AjaxResult
        return toAjax(isSuccess);
    }

    //修改柜机类型数据
    @Operation(summary = "修改柜机类型")
    @RequiresPermissions("device:cabinetType:edit")
    @Log(title = "柜机类型",businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateById(@RequestBody CabinetType cabinetType){
        //使用mp
        boolean isSuccess = cabinetTypeService.updateById(cabinetType);
        //方法用于更新 删除等无返回数据的操作，判断是否成功 返回的仍然是AjaxResult
        return toAjax(isSuccess);
    }

    //删除 可根据多个id批量删除
    @Operation(summary = "删除柜机类型")
    @RequiresPermissions("device:cabinetType:remove")
    @Log(title = "柜机类型",businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable List<Long> ids){
        //使用mp批量删除
        boolean isSuccess = cabinetTypeService.removeBatchByIds(ids);
        //方法用于更新 删除等无返回数据的操作，判断是否成功 返回的仍然是AjaxResult
        return toAjax(isSuccess);
    }

    //获取全部柜机类型
    @Operation(summary = "查询全部柜机类型列表")
    @GetMapping("/getCabinetTypeList")
    public AjaxResult getCabinetTypeList(){
        //mp 封装的查询所有数据方法
        List<CabinetType> cabinetTypeList = cabinetTypeService.list();
        return AjaxResult.success(cabinetTypeList);
    }
}
